package com.xuexiang.xuidemo.slice;

import com.xuexiang.xui_lib.component.TitleBarLayout;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;


public class BaseAbilitySlice extends AbilitySlice implements TitleBarLayout.ITitleBarClickListener {

    protected TitleBarLayout mTitleBar;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        int layout = getUIContentLayoutId();
        if (layout > 0) {
            ComponentContainer root = (ComponentContainer) LayoutScatter.getInstance(this).parse(layout, null, false);
            setUIContent(root);
        }
        int titleBarId = getTitleBarId();
        if (titleBarId > 0) {
            mTitleBar = (TitleBarLayout) findComponentById(titleBarId);
            mTitleBar.setTitleClickListener(this);
        }
        initComponents();
    }


    protected void initComponents() {

    }

    protected int getUIContentLayoutId() {
        return -1;
    }

    public String getString(int resId) {

        try {
            return getResourceManager().getElement(resId).getString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public int getColor(int colorId) {
        try {
            return getResourceManager().getElement(colorId).getColor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public FractionManager getFractionManager() {
        Ability ability = getAbility();
        if (ability instanceof FractionAbility) {
            FractionAbility fractionAbility = (FractionAbility) ability;
            return fractionAbility.getFractionManager();
        }
        return null;
    }

    protected int getTitleBarId() {
        return -1;
    }

    @Override
    public void onClick(Component component, TitleBarLayout.ClickEventType type) {
        switch (type) {
            case LEFT_ICON_CLICK:
                terminate();
                break;
            case RIGHT_ICON_CLICK:
                break;
            default:
                break;
        }
    }
}
